﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[Get_SetDateTime]
(
)
RETURNS DATETIME AS
Begin

	Declare @dtSet int --状态 0=关闭 1=开启:4点之后操作时间+12小时
	Select TOP 1 @dtSet= DateTimeSet FROM DateTimeConfig 
	
	Declare @nDays int,@nHour int,@nMinute int,@dToday DateTime
	Set @dToday = GetDate()

	Set @nHour = 16
	Set @nMinute = 30

--	If Month(GetDate())<12
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())) + '-' + Convert(varchar(2),Month(GetDate())+1) + '-1'))
--	Else
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())+1) + '-01-01'))

	--Set @nDays = DateDiff(d,GetDate(),Dateadd(dd,-DatePart(dd,GetDate()) ,DateAdd(mm,1,GetDate()))) 原来的
	--2016-1-31日修改所有GetDate()改为@dToday
	Set @nDays = DateDiff(d,@dToday,dateadd(dd,-day(dateadd(m,1,@dToday)),dateadd(m,1,@dToday)));

	If @nDays>0 And @dtSet=1
	Begin
		If DatePart(hh,@dToday)>@nHour Or (DatePart(hh,@dToday)=@nHour And DatePart(mi,@dToday)>@nMinute)
			Set @dToday = DateAdd(hh,12,@dToday)			
	End

	Return @dToday
End